loadstring(exports.dgs:dgsImportFunction())()

local sw,sh = guiGetScreenSize()
local px,py = sw/1366,sh/768
cmtaPRZEBIERALNIA = false
font = dgsCreateFont( ":cmta-pobieraniezasobow/OSB.ttf", 12*px )

skinyzwykle = {

    {2,500},
    {7,500},
    {11,500},
    {12,500},
    {14,500},
    {15,500},
    {17,500},
    {18,500},
    {19,500},
    {20,500},
    {21,500},
    {22,500},
    {23,500},
    {24,500},
    {25,500},
    {28,500},
    {29,500},
    {30,500},
    {33,500},
    {36,500},
    {37,500},
    {40,500},
    {45,500},
    {46,500},
    {48,500},
    {55,500},
    {56,500},
    {59,500},
    {61,500},
    {63,500},
    {76,500},
    {94,500},
    {97,500},
    {98,500},
    {101,500},
    {143,500},
    {146,500},
    {147,500},
    {154,500},

}

skinyvip = {
   {274,2100},
   {275,2700},
   {276,2200},
   {278,2400},
   {279,2400},
   {280,1800},
   {284,2200},
   {285,1900},
   {286,2500},
   {288,2000},
   {301,1600},
   {302,2300},
   {310,1800},
   {311,1500},
}

skinyunikat = {

}

addEvent("cmta:przebieralnia:showGUI", true)
addEventHandler("cmta:przebieralnia:showGUI", resourceRoot, function(result)
    if cmtaPRZEBIERALNIA == false then
        cmtaPRZEBIERALNIA = true
        showCursor(true)
        addEventHandler ( "onDgsMouseClick", getRootElement(), onclick)
        window = dgsCreateWindow(1100*px,300*py,250*px,450*py,"Przebieralnia",false)
        dgsWindowSetCloseButtonEnabled(window, false)
        dgsWindowSetMovable (window, false )
        dgsWindowSetSizable (window, false )
        tabpanel = dgsCreateTabPanel (0*px, 0*py, 250*px, 290*py,false,window)
        tab1 = dgsCreateTab("Sklep                      ",tabpanel,false,false,false,false,false,false,false,false,tocolor(90,90,90),tocolor(210,105,30,255),tocolor(210,105,30,255))
        tab2 = dgsCreateTab("Twoje skiny             ",tabpanel,false,false,false,false,false,false,false,false,tocolor(90,90,90),tocolor(210,105,30,255),tocolor(210,105,30,255))
        button = dgsCreateButton( 25*px, 300*py, 200*px, 40*py, "Kup", false,tab1,false,false,false,false,false,false,false,tocolor(90,90,90),tocolor(210,105,30,255))
        button2 = dgsCreateButton( 25*px, 350*py, 200*px, 40*py, "Zamknij", false,tab1,false,false,false,false,false,false,false,tocolor(90,90,90),tocolor(210,105,30,255))
        button3 = dgsCreateButton( 25*px, 300*py, 200*px, 40*py, "Ubierz", false,tab2,false,false,false,false,false,false,false,tocolor(90,90,90),tocolor(210,105,30,255))
        button4 = dgsCreateButton( 25*px, 350*py, 200*px, 40*py, "Zamknij", false,tab2,false,false,false,false,false,false,false,tocolor(90,90,90),tocolor(210,105,30,255))
        gridbuy = dgsCreateGridList (0*px, 0*py, 250*px, 290*py, false,tab1 )
        dgsGridListAddColumn( gridbuy, "Skin", 0.6 ) 
        dgsGridListAddColumn( gridbuy, "Koszt", 0.3 )
        dgsGridListSetAutoSortEnabled(gridbuy,false)
        mygrid = dgsCreateGridList (0*px, 0*py, 250*px, 290*py, false,tab2 )
        dgsGridListAddColumn( mygrid, "Skin", 0.96 ) 
        dgsGridListSetAutoSortEnabled(mygrid,false)
        lista(result)

        setCameraMatrix(215.47720, -99.87910, 1006.25781,217.88373, -98.33634, 1005.25781)
        show_ped=createPed(0,217.88373, -98.33634, 1005.25781,0)
        setElementRotation(show_ped,0,0,120)
        setElementInterior(show_ped,15)
        setElementDimension(show_ped,2000)


    end
end)

function lista(result)
    dgsGridListClear(gridbuy)
    dgsGridListClear(mygrid)

    for x,z in pairs(result) do
        local row = dgsGridListAddRow ( mygrid )
        dgsGridListSetItemText ( mygrid, row, 1, z )
    end

    local row0 = dgsGridListAddRow ( gridbuy )
    dgsGridListSetItemText ( gridbuy, row0, 1, "❋ ❋ skiny zwykłe ❋ ❋" )
    dgsGridListSetRowSelectable(gridbuy, row0, false)

    for i,v in pairs(skinyzwykle) do
        local row = dgsGridListAddRow ( gridbuy )
        dgsGridListSetItemText ( gridbuy, row, 1, v[1] )
        dgsGridListSetItemText ( gridbuy, row, 2, v[2] )
        for _,z in pairs(result) do
        if v[1] == tonumber(z) then
            dgsGridListRemoveRow ( gridbuy, row )
        end
        end

    end

    local row1 = dgsGridListAddRow ( gridbuy )
    dgsGridListSetItemText ( gridbuy, row1, 1, "✩ ✩ ✩  skiny V.I.P  ✩ ✩ ✩" )
    dgsGridListSetItemColor (gridbuy, row1, 1 , 217, 115, 25)
    dgsGridListSetRowSelectable(gridbuy, row1, false)

    for i,v in pairs(skinyvip) do
        if v[1] ~= tonumber(resultz) then
            local row = dgsGridListAddRow ( gridbuy )
            dgsGridListSetItemText ( gridbuy, row, 1, v[1] )
            dgsGridListSetItemText ( gridbuy, row, 2, v[2] )
            dgsGridListSetItemData ( gridbuy, row, 1, "vip" )
            for _,z in pairs(result) do
                if v[1] == tonumber(z) then
                    dgsGridListRemoveRow ( gridbuy, row )
                end
            end
        end
    end    
    
    for i,v in pairs(skinyunikat) do
    if v then
    local row2 = dgsGridListAddRow ( gridbuy )
    dgsGridListSetItemText ( gridbuy, row2, 1, "◄ ◄ skiny UNIKATOWE ► ►" )
    dgsGridListSetItemColor (gridbuy, row2, 1 , 255, 0, 0)
    dgsGridListSetRowSelectable(gridbuy, row2, false)
    end
    end
    

end

function onclick(btn,state)
    if btn=="left" and state=="down" and cmtaPRZEBIERALNIA then
    
    if source == tab1 then
        dgsGridListSetSelectedItem( gridbuy, -1 )
    end
    
    if source == tab2 then
        dgsGridListSetSelectedItem( mygrid, -1 )
    end

    if source == button2 or source == button4 then
        removeEventHandler ( "onDgsMouseClick", getRootElement(), onclick)
        cmtaPRZEBIERALNIA = false
        showCursor(false)
        destroyElement(window)
        destroyElement(show_ped)
        setCameraTarget(localPlayer,localPlayer)	
    end
    
    if source == gridbuy then
        local selected = dgsGridListGetSelectedItem(gridbuy)
		if selected ~=-1 then
            if tonumber(dgsGridListGetItemText(gridbuy,selected,1)) then
			setElementModel(show_ped,dgsGridListGetItemText(gridbuy,selected,1))
            end
        end
    end

    if source == mygrid then
        local selected = dgsGridListGetSelectedItem(mygrid)
		if selected ~=-1 then
            if tonumber(dgsGridListGetItemText(mygrid,selected,1)) then
			setElementModel(show_ped,dgsGridListGetItemText(mygrid,selected,1))
            end
        end
    end

    if source == button then
        local selected = dgsGridListGetSelectedItem(gridbuy)
		if selected ~=-1 then
        if tonumber(dgsGridListGetItemText(gridbuy,selected,1)) then
        if dgsGridListGetItemData(gridbuy,selected,1) == "vip" and not getElementData(localPlayer,"player:data").vip then exports['cmta-notyfikacje']:sendNotification("#FF0000Wykryto nieprawidłowość!\n#ffffffNie posiadasz konta V.I.P","error",4000,2) return end
        if tonumber(dgsGridListGetItemText(gridbuy,selected,2)) > tonumber(getElementData(localPlayer,"player:data").points) then exports['cmta-notyfikacje']:sendNotification("#FF0000Wykryto nieprawidłowość!\n#ffffffNie posiadasz tylu punktów.","error",4000,2) return end
        triggerServerEvent("cmta:przebieralnia:kup", resourceRoot,dgsGridListGetItemText(gridbuy,selected,1),dgsGridListGetItemText(gridbuy,selected,2))
        end
        end
    end
    if source == button3 then
        local selected = dgsGridListGetSelectedItem(mygrid)
		if selected ~=-1 then 
            if tonumber(dgsGridListGetItemText(mygrid,selected,1)) then
                timer = timer or 0 
                timerb = getTickCount()
                if timer > timerb then return exports['cmta-notyfikacje']:sendNotification("#FF0000Wykryto nieprawidłowość!\n#ffffffOdczekaj chwilę...","error",4000,2) end
                timer = getTickCount()+3000
                triggerServerEvent("cmta:przebieralnia:ubierz", resourceRoot,dgsGridListGetItemText(mygrid,selected,1))
            end
        end
    end
end
end

addEvent("cmta:przebieralnia:refresh", true)
addEventHandler("cmta:przebieralnia:refresh", resourceRoot, function(result)
lista(result)
end)